package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.ChargeClazzPremium;
import com.gljx.web.Entity.ChargeClazzPremiumEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.ChargeClazzPremiumEntryMapper;
import com.gljx.web.dao.ChargeClazzPremiumMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.service.ChargeClazzPremiumService;

@Service
@Transactional
public class ChargeClazzPremiumServiceImpl extends CommonDao implements ChargeClazzPremiumService{

	@Autowired
	private ChargeClazzPremiumEntryMapper chargeClazzPremiumEntryMapper;
	
	@Autowired
	private ChargeClazzPremiumMapper chargeClazzPremiumMapper;
	
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.ChargeClazzPremiumMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.ChargeClazzPremiumMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<ChargeClazzPremiumEntry> selectChargeClazzPremiumEntry(String fparentId, String studentName,
			String cfidcardno, String cfmobile) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("v_fparentid", fparentId);
	       paramMap.put("v_studentName", studentName);
	       paramMap.put("v_cfidcardno", cfidcardno);
	       paramMap.put("v_cfmobile", cfmobile);
	       paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.ChargeClazzPremiumEntryMapper.selectChargeClazzPremiumEntry", paramMap, null);
	       @SuppressWarnings("unchecked")
		   List<ChargeClazzPremiumEntry>chargeClazzPremiumEntrys = (List<ChargeClazzPremiumEntry>) paramMap.get("v_cursor");
	       return chargeClazzPremiumEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editChargeClazzPremium(ChargeClazzPremium chargeClazzPremium, String[] fseq, String[] cfstudentno,
			String[] cfstudentid, String[] cfidcardno, String[] cfsex,String[] cfmobile, String[] cfrxrq, String[] cfcurrentclassid,
			String[] cfamount, String[] cfshje, String[] cfreturninclassid, String[] cfzrbjamount, String[] cftfje,
			String[] cfremark, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(chargeClazzPremium.getFid()) || chargeClazzPremium.getFid() == null){
			//创建人id
			chargeClazzPremium.setFcreatorid(user.getFid());
			//利用UUID生成fid
			chargeClazzPremium.setFid(GetFid.UUIDIntoFid());
			//当前时间
			chargeClazzPremium.setFcreatetime(time);
			//状态
            chargeClazzPremium.setCfbillstate(3L);
            //添加
			chargeClazzPremiumMapper.insertSelective(chargeClazzPremium);
		}else{
			//修改时间时间
			chargeClazzPremium.setFlastupdatetime(time);
			//修改人id
			chargeClazzPremium.setFlastupdateuserid(user.getFid());
			//修改
			chargeClazzPremiumMapper.updateByPrimaryKeySelective(chargeClazzPremium);
			//先删除子类信息
			chargeClazzPremiumEntryMapper.deleteByFparentId(chargeClazzPremium.getFid());
		}
		//添加子类
		List<ChargeClazzPremiumEntry> chargeClazzPremiumEntrys = new ArrayList<>();
		if(fseq == null || fseq.length == 0){
					
		}else{
			for (int i = 0; i < fseq.length; i++) {
				ChargeClazzPremiumEntry chargeClazzPremiumEntry = new ChargeClazzPremiumEntry();
				chargeClazzPremiumEntry.setCfstudentno(cfstudentno[i]);
				chargeClazzPremiumEntry.setCfstudentid(cfstudentid[i]);
				chargeClazzPremiumEntry.setCfidcardno(cfidcardno[i]);
				if("".equals(cfsex[i])){
					
				}else{
					chargeClazzPremiumEntry.setCfsex(Long.parseLong(cfsex[i]));
				}
				chargeClazzPremiumEntry.setCfmobile(cfmobile[i]);
				chargeClazzPremiumEntry.setCfrxrq(cfrxrq[i]);
				chargeClazzPremiumEntry.setCfcurrentclassid(cfcurrentclassid[i]);
				if("".equals(cfamount[i])){
					
				}else{
					chargeClazzPremiumEntry.setCfamount(new BigDecimal(cfamount[i]));
				}
				if("".equals(cfshje[i])){
					
				}else{
					chargeClazzPremiumEntry.setCfshje(new BigDecimal(cfshje[i]));
				}
				chargeClazzPremiumEntry.setCfreturninclassid(cfreturninclassid[i]);
				if("".equals(cfzrbjamount[i])){
					
				}else{
					chargeClazzPremiumEntry.setCfzrbjamount(new BigDecimal(cfzrbjamount[i]));
				}
				if("".equals(cftfje[i])){
					
				}else{
					chargeClazzPremiumEntry.setCftfje(new BigDecimal(cftfje[i]));
				}
				chargeClazzPremiumEntry.setCfremark(cfremark[i]);
				chargeClazzPremiumEntry.setFseq((long) i+1);
				chargeClazzPremiumEntry.setFid(GetFid.UUIDIntoFid());
				chargeClazzPremiumEntry.setFparentid(chargeClazzPremium.getFid());
				chargeClazzPremiumEntrys.add(chargeClazzPremiumEntry);
			}
			chargeClazzPremiumEntryMapper.insertChargeClazzPremiumEntry(chargeClazzPremiumEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public ChargeClazzPremium queryChargeClazzPremiumByFid(String fid) {
		// TODO Auto-generated method stub
		return chargeClazzPremiumMapper.selectChargeClazzPremiumByFid(fid);
	}

	/**
	 * 删除
	 */
	@Override
	public void deleteChargeClazzPremiumByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			//先删除子类信息
			chargeClazzPremiumEntryMapper.deleteByArray(deleteId);
			//在删除父类信息
			chargeClazzPremiumMapper.deleteByArray(deleteId);
		}else{
			//先删除子类信息
			chargeClazzPremiumEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			chargeClazzPremiumMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 审核，反审核
	 */
	@Override
	public int auditChargeClazzPremium(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
		// TODO Auto-generated method stub
		ChargeClazzPremium chargeClazzPremium = new ChargeClazzPremium();
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
			if (serverResponse.getStatus() == 1) {
				//fid
				chargeClazzPremium.setFid(fid);
				//审核人
				chargeClazzPremium.setFauditorid(fhandlerId);
				//修改人
				chargeClazzPremium.setFlastupdateuserid(fhandlerId);
				chargeClazzPremium.setCfbillstate(5L);
				//结束
				return chargeClazzPremiumMapper.updateByPrimaryKeySelective(chargeClazzPremium);
			} else if (serverResponse.getStatus() == 2) {
				if (!"1".equals(state)) {
					chargeClazzPremium.setFid(fid);
					chargeClazzPremium.setCfbillstate(3L);
					chargeClazzPremiumMapper.updateByPrimaryKeySelective(chargeClazzPremium);
				}
				//推进
				return 2;
			} else {
				//错误
				return 0;
			}
		}else{
            axamine.againstAuditProcess(fnumber, message);
            chargeClazzPremium.setCfbillstate(3L);
			chargeClazzPremium.setFid(fid);
			return chargeClazzPremiumMapper.updateByPrimaryKeySelective(chargeClazzPremium);
		}

	}

    @Override
    public int modifyChargeClazzPremium(ChargeClazzPremium chargeClazzPremium) {
        return chargeClazzPremiumMapper.updateByPrimaryKeySelective(chargeClazzPremium);
    }

    @Override
    public List<ChargeClazzPremiumEntry> selectChargeClazzPremiumByInfo(String fcompanyid, String studentName, String cfidcardno, String cfmobile, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("cfidcardno", cfidcardno);
        map.put("cfmobile", cfmobile);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return chargeClazzPremiumEntryMapper.selectChargeClazzPremiumByInfo(map);
    }
}
